package xxl.robotlegs.extensions.viewmanager 
{
	import xxl.robotlegs.extensions.viewmanager.impl.ContainerRegistry;
	import xxl.robotlegs.extensions.viewmanager.impl.StageObserver;
	import xxl.robotlegs.framework.api.IContext;
	import xxl.robotlegs.framework.api.IExtension;
	import xxl.robotlegs.framework.api.IInjector;
	import xxl.robotlegs.framework.api.ILogger;
	
	/**
	 * ...
	 * @author XXL
	 */
	public class StageObserverExtension implements IExtension 
	{
		private static var _INSTALL_COUNT:int = 0;
		private static var _STAGE_OBSERVER:StageObserver;
		
		private var _injector:IInjector;
		private var _logger:ILogger;
		
		public function StageObserverExtension() 
		{
			
		}
		
		private function whenInitializing():void
		{
			if (!_STAGE_OBSERVER)
			{
				const containerRegistry:ContainerRegistry = _injector.getInstance(ContainerRegistry);
				_STAGE_OBSERVER = new StageObserver(containerRegistry);
				_logger.debug("创建了一个StageObserver");
			}
		}
		
		private function whenDestroying():void
		{
			_INSTALL_COUNT--;
			if (_INSTALL_COUNT == 0)
			{
				_logger.debug("销毁了StageObserver");
				_STAGE_OBSERVER.destroy();
				_STAGE_OBSERVER = null;
			}
		}
		
		/* INTERFACE xxl.robotlegs.framework.api.IExtension */
		
		public function extend(context:IContext):void 
		{
			context.whenInitializing(whenInitializing);
			context.whenDestroying(whenDestroying);
			_INSTALL_COUNT++;
			_injector = context.injector;
			_logger = context.getLogger(this);
		}
		
	}

}